<?php
date_default_timezone_set('Asia/Shanghai');//时区配置
$conn=mysql_connect("localhost","root","gsda1.mysql1");
include 'functions.php';
mysql_select_db("new_gsdai", $conn);
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);

$end = strtotime('2013-08-07 09:00:00');
$sql = "SELECT user_id,invite_userid FROM ych_user WHERE add_time>$end and invite_money=20 AND invite_userid>0 AND user_id IN 
		(SELECT user_id FROM ych_borrow_tender WHERE borrow_id IN (SELECT id FROM ych_borrow WHERE fstatus=3 AND ftype!=5)
		GROUP BY user_id HAVING(SUM(account)>=10000))";
$result = db_fetch_arrays($sql, $conn);
$index = 0;
foreach ($result as $key => $value){
	$query = mysql_query("update ych_user SET invite_money=0 WHERE user_id={$value['user_id']}", $conn);
	if(!$query){
		echo $sql;
		exit;
	}
	
	$account_result_reward 	= db_fetch_array("select * from ych_account where user_id={$value['invite_userid']}", $conn);
	$log['user_id'] 		= $value['invite_userid'];
	$log['ftype'] 			= 46;
	$log['money'] 			= 20;
	$log['total'] 			= $account_result_reward['total']-$log['money'];
	$log['use_money'] 		= $account_result_reward['use_money']-$log['money'];
	$log['no_use_money'] 	= $account_result_reward['no_use_money']?$account_result_reward['no_use_money']:0.00;
	$log['collection'] 		= $account_result_reward['collection']?$account_result_reward['collection']:0.00;
	$log['to_user'] 		= "1";
	$log['remark'] 			= "扣除发放错误的邀请奖励";
	$log['add_time'] 		= time();
	$log['addip'] 			= '127.0.0.1';
	$mode = $value['invite_userid']%100;
	$sql  ="insert into ych_account_log_$mode(user_id,ftype,total,money,use_money,no_use_money,
													collection,to_user,remark,add_time,addip) values( 
				 {$value['invite_userid']},{$log['ftype']},{$log['total']},{$log['money']},
				 {$log['use_money']},{$log['no_use_money']},
				 {$log['collection']},{$log['to_user']},'{$log['remark']}',
				 {$log['add_time']},'{$log['addip']}'
				 )";
	$query = mysql_query($sql, $conn);
	if(!$query){
		echo $sql;
		exit;
	}
	$query = mysql_query("update ych_account set total=total-{$log['money']},
				 use_money=use_money-{$log['money']} where user_id={$value['invite_userid']}", $conn);
	if(!$query){
		echo $sql;
		exit;
	}
	$index++;
}
echo $index;
